package com.live.dengdengtv.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by wyf on 2016/9/8.
 */
public class DateUtils {
    private static final SimpleDateFormat defaultFmat =
            new SimpleDateFormat("yyyy-M-d", Locale.getDefault());

    //HH:mm:ss
    public static final String yyyy_M_d = "yyyy-M-d";
    /**
     * 字符串转换成日期
     *
     * @param str
     * @return date
     */
    public static Date strToDate(String str, String format) {

//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            SimpleDateFormat formatter = new SimpleDateFormat (format, Locale.getDefault());
            Date date = formatter.parse(str);
            return date;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String dateToString(Date date, String format){
        String dateString = null;
        try {
            SimpleDateFormat formatter = new SimpleDateFormat (format, Locale.getDefault());
            dateString = formatter.format(date);
            return dateString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 判断日期是否符合指定的格式
     * @param s
     * @param format 示例:"yyyy-MM-dd"
     * @return
     */
    public static boolean isValidDate(String s, SimpleDateFormat format) {

        format.setLenient(false);
        try {
            format.parse(s);
            return true;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return false;
        }
    }
}
